// logs.ts
// const util = require('../../utils/util.js')
const api = require('../../utils/api');
// import { formatTime } from '../../utils/util'
// index.js
Page({
    data: {
        searchValue: '',
        pageNum: 1,
        pageSize: 20,
        noMoreData: false, // 标记是否还有更多数据
        currentTab: 0,
        goodsTypeList: [],
        goodsType: '',
        list: [
            //       {
            //       goodsName:'商品1',
            //       goodsCurrentPrice:'100',
            //       goodsPicture:'12',
            //   }
        ]
    },
    switchTab(e) {
        const index = e.currentTarget.dataset.dictIndex;
        this.setData({
            currentTab: index,
            goodsType: e.currentTarget.dataset.dictValue
        });
        this.getSearchFn()
    },
    godetailFn(event) {
        const goodsId = event.currentTarget.dataset.goodsId;
        wx.navigateTo({
            url: '/pages/goodsdetail/goodsdetail?goodsId=' + goodsId
        })
    },
    onShow(){
        this.goodsTypeFn()
    },
    onLoad() {
        this.getGoodsList()
    },
    goodsTypeFn() {
        api.dictType('goods_type').then(res => {
            if (res.code == 200) {
                const updatedGoodsTypeList = [{
                    dictCode: "666",
                    dictLabel: "全部",
                    dictValue: ""
                }, ...res.data];
                this.setData({
                    goodsTypeList: updatedGoodsTypeList
                })
            }
        })
    },
    getSearchFn() {
        this.setData({ noMoreData: false, pageNum: 1, list: [] });
        this.getGoodsList()
    },
    getGoodsList() {
        if (this.data.noMoreData) return;
        let _params = {
            goodsType: this.data.goodsType,
            goodsName: this.data.searchValue,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
        }
        api.goodsList(_params).then(res => {
            if (res.code == 200) {
                const newData = res.data.records;
                if (newData.length < this.data.pageSize) {
                    this.setData({ noMoreData: true });
                }
                this.setData({
                    list: this.data.list.concat(newData),
                    pageNum: this.data.pageNum + 1
                })
            }
        })
    },
    onReachBottom: function () {
        this.getGoodsList();
    },
    onSearchInput(e) {
        this.setData({
            searchValue: e.detail.value
        })
    },
    onShareAppMessage() {

    }
})
// Component({
//   data: {
//     logs: [],
//   },
//   lifetimes: {
//     attached() {
//       this.setData({
//         logs: (wx.getStorageSync('logs') || []).map((log: string) => {
//           return {
//             date: formatTime(new Date(log)),
//             timeStamp: log
//           }
//         }),
//       })
//     }
//   },
// })
